/* eslint-disable semi */
/**
 * layer 管理
 */

import sgisMapBase from "./sgis.map.base";
import {
  googleTiledMap
} from "./lib/leaflet-extends/google.layer";
import {
  mapabcTiledMap
} from "./lib/leaflet-extends/mapabc.layer";
export default {
  baseLayer: {}, // 底图
  currLayerId: "", // 当前图层id
  baseLayers: [], // 所有图层
  overLayers: [],
  mapInformation: [], // 图层信息
  init(options) {
    this.mapInformation = options;
    // ////分割线//////////
    this.initLayer(); // 将其他图层初始化
    sgisMapBase.getMap().options.crs = this.calCRs(0);
    // 根据图层设置容器
    const op = this.calMapOptions(0);
    sgisMapBase.getMap().setView(op.center, op.zoom);
    sgisMapBase.getMap().setMinZoom(op.minZoom);
    sgisMapBase.getMap().setMaxZoom(op.maxZoom);
    // 根据图层设置容器
    this.baseLayer = this.baseLayers[0].layer; // 获取底图图层
    this.currLayerId = this.mapInformation[0].id; // 当前图层id
    this.baseLayer.addTo(sgisMapBase.getMap());
    // 增加图例控件
    // if (!$('#layerControl').length) {
    //   LayerControl.loadtpl();
    // }
  }, // 图层初始化(容器会一并初始化在这里)
  getBaseLayer() {
    return this.baseLayer;
  },
  initLayer() {
    const layers = [];
    for (let i = 0; i < this.mapInformation.length; i++) {
      const layer = this.formatLayer(i);
      layers.push({
        layer,
        layerName: this.mapInformation[i].name,
        layerId: this.mapInformation[i].id,
        layerType: "baselayer"
      });
    }
    this.baseLayers = layers;
  }, // 初始化所有图层
  setZIndexUp(layerid) {
    const layer = this.getOverLayerById(layerid);
    layer.setZIndexOffset(1);
    return;
  }, // 未封装
  getOverLayers() {
    return this.overLayers;
  }, // 获取所有除底图外的覆盖图层
  removeOverLayer(layerId) {
    // layerId 删除图层的id
    for (const key in this.overLayers) {
      if (this.overLayers[key].layerId === layerId) {
        sgisMapBase.getMap().removeLayer(this.overLayers[key].layer)
        if (this.overLayers[key].hasOwnProperty('otherLayer')) {
          sgisMapBase.getMap().removeLayer(this.overLayers[key].otherLayer)
        }
        this.overLayers.splice(key, 1);
        // LayerControl.layerChange(this.getOverLayers(), sgisMapBase.getMap());
        return;
      }
    }
  }, // 移除其他图层信息
  // layerControlChange() {
  //   LayerControl.layerChange(this.getOverLayers(), sgisMapBase.getMap());
  // },
  setOverLayers(options) {
    /*
     options:{
        layer:layer, // 图层信息
        layerName: 'xxx',
        layerId: id,
        layerType: "overlayer"
      }
    */
    if (!options.hasOwnProperty('show')) {
      options.show = true;
    }
    options.layerType = "overlayer";
    this.overLayers.push(options);
    // sgisMapBase.getMap().scrollWheelZoom = false;
    // LayerControl.layerChange(this.getOverLayers(), sgisMapBase.getMap());
  }, // 设置其他图层
  getAllLayersInfo() {
    return this.baseLayers;
  }, // 获取所有的覆盖图层
  getLayer(layerid) {
    for (const key in this.baseLayers) {
      if (this.baseLayers[key].layerId === layerid) {
        return this.baseLayers[key];
      }
    }
  }, // 根据layerid获取地图layer
  getOverLayerById(layerid) {
    for (const key in this.overLayers) {
      if (this.overLayers[key].layerId === layerid) {
        return this.overLayers[key];
      }
    }
  }, // 根据layerid获取overLayer
  hideLayer(layerid) {
    const layer = this.getLayer(layerid);
    const map = sgisMapBase.getMap();
    try {
      map.removeLayer(layer);
    } catch (err) {
      console.log(err);
    }
  }, // 隐藏图层
  showLayer(layerid) {
    const layer = this.getLayer(layerid);
    const map = sgisMapBase.getMap();
    layer.addTo(map);
    this.baseLayer = layer;
  }, // 显示图层
  switchBaseLayer(layerId) {
    let key = '';
    for (const index in this.baseLayers) {
      if (this.baseLayers[index].layerId === layerId) {
        key = index;
        break;
      }
    }
    const map = sgisMapBase.getMap();
    delete map.options.crs;
    map.options.crs = this.calCRs(key);
    map.removeLayer(this.baseLayer);
    // const options = this.calMapOptions(key);
    // map.setView(options.center, options.zoom);
    // map.setMinZoom(options.minZoom);
    // map.setMaxZoom(options.maxZoom);
    this.baseLayer = this.formatLayer(key); // 获取图层
    this.baseLayer.addTo(map);
  }, // 图层切换
  calMapOptions(key) {
    const zoom = this.mapInformation[key].zoom ?
      this.mapInformation[key].zoom :
      this.mapInformation[key].minzoom;
    const mapContainerOptions = {
      zoom,
      minZoom: this.mapInformation[key].minzoom,
      maxZoom: this.mapInformation[key].maxzoom,
      center: L.latLng([
        this.mapInformation[key].centx,
        this.mapInformation[key].centy
      ])
    };
    return mapContainerOptions;
  }, // 格式化地图参数
  calCRs(key) {
    if (this.mapInformation[key].customcrs === 0) {
      const crs = L.CRS[this.mapInformation[key].crscode];
      return crs;
    } else {
      const crs = L.Proj.CRS(this.mapInformation[key].epsgcode, {
        origin: this.mapInformation[key].origin,
        scaleDenominators: this.mapInformation[key].scaledenominators,
        resolutions: this.mapInformation[key].resolutions,
        bounds: L.bounds(
          this.mapInformation[key].bounds[0],
          this.mapInformation[key].bounds[1]
        )
      });
      return crs;
    }
  }, // 投影坐标系
  formatLayer(key) {
    const type = this.mapInformation[key].server_type;
    const crs = this.calCRs(key);
    switch (type) {
      case "REST":
        return L.supermap.tiledMapLayer(this.mapInformation[key].url, {
          crs
        });
      case "TDT":
        // 不支持crs
        return L.supermap.tiandituTileLayer({
          crs,
          url: 'http://t{s}.tianditu.com/{layer}_{proj}/wmts?tk=fb6e2af6aa18677182d8c15215d3e83f'
        });
      case "WMTS":
        // 不支持crs
        return L.supermap.wmtsLayer(this.mapInformation[key].url, {
          ...this.mapInformation[key].options,
          crs
        });
      case "WMS":
        return L.tileLayer.wms(this.mapInformation[key].url, {
          ...this.mapInformation[key].options,
          crs
        });
      case "MAPABC":
        return mapabcTiledMap.tiledLayer(type, {
          ...this.mapInformation[key].options,
          crs
        });
      case "GOOGLE":
        return googleTiledMap.tiledLayer(type, {
          ...this.mapInformation[key].options,
          crs
        });
      default:
        break;
    }
  }, // 格式化图层参数
  getCurrenScale() {
    const SMType = this.mapInformation[this.currLayerId].server_type;
    const map = sgisMapBase.getMap();
    const zoom = map.getZoom();
    switch (SMType) {
      case "REST":
        return this.baseLayer.scales[zoom];
      case "TDT":
        return this.baseLayer.options.crs.scales[zoom];
      case "WMTS":
        return this.baseLayer.options.crs.scales[zoom];
      case "WMS":
        return this.baseLayer.options.crs.scales[zoom];
      case "MAPABC":
        return this.baseLayer.options.crs.scale(map.getZoom());
      case "GOOGLE":
        return this.baseLayer.options.crs.scale(map.getZoom());
      default:
        return;
    }
  } // 获取地图当前缩放比例
};
